package cn.eonshine.cuba.inf.modular.quartz.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;

import cn.eonshine.base.core.base.controller.BaseController;
import cn.eonshine.base.core.util.LocaleStringUtil;
import cn.eonshine.cuba.inf.modular.quartz.config.ConfigEnum;
import cn.eonshine.cuba.inf.modular.quartz.entity.Config;
import cn.eonshine.cuba.inf.modular.quartz.entity.Result;
import cn.eonshine.cuba.inf.modular.quartz.service.IQuartzTableService;

/**
 * @Description: :
 * @date :Create in  2018/2/24- 11:14
 * @Version: V1.0
 * @Modified By:
 **/
@Controller
@RequestMapping({"/quartz","/rest/quartz"})
public class QuartzTable extends BaseController {

//    private final Logger LOGGER=LoggerFactory.getLogger(this.getClass());

    private String PREFIX = "/quartz/config/";
    
    @Autowired
    private IQuartzTableService tableService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "config.html";
    }
    
    @RequestMapping("/config_add")
    public String configAdd() {
        return PREFIX + "config_add.html";
    }

    @RequestMapping("/config_update/{configId}")
    public String configUpdate(@PathVariable String configId, Model model) {
        Config config = getConfigObjectById(configId);
        model.addAttribute("item",config);
        return PREFIX + "config_edit.html";
    }
    
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	 List<Config> configs = listForIntegrate(condition);
         for (Config config : configs) {
             String message = LocaleStringUtil.getMessage(ConfigEnum.findToMessage(config.getStatus()));
             config.setStatus(message);
         }
        return configs;
    }
    
    @RequestMapping(value = "/integrateList")
    @ResponseBody
    public List<Config> listForIntegrate(String condition) {
    	 List<Config> configs = Lists.newArrayList();
    	 if(StringUtils.isBlank(condition)) {
    		 configs = tableService.getAllJobs();
    	 }else {
    		 QueryWrapper<Config> wrapper = new QueryWrapper<Config>();
    		 configs = tableService.list(wrapper.like("C_NAME", condition));
    	 }
        return configs;
    }
    
    

    @RequestMapping("/getConfig/{configId}")
    @ResponseBody
    public Config getConfigObjectById(@PathVariable String configId) {
    	Config config = tableService.getById(configId);
    	return config;
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Config config) {
    	tableService.save(config);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String configId) {
    	tableService.removeById(configId);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Config config) {
    	tableService.updateById(config);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/detail/{configId}")
    @ResponseBody
    public Object detail(@PathVariable("configId") Integer configId) {
        return tableService.getById(configId);
    }
    
    @GetMapping("/table")
    public String table(ModelMap map){
        List<Config> configs = tableService.getAllJobs();
        for (Config config : configs) {
            String message = ConfigEnum.findToMessage(config.getStatus());
            config.setStatus(message);
        }
        map.put("configs",configs);
        return "/table";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(@RequestParam String id,@RequestParam String status, @RequestParam String updateBy){
        Result result = new Result();
        result.setResult(false);
        try {
            tableService.update(id, status, updateBy);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }
    
    @RequestMapping("/updateQuartz")
    @ResponseBody
    public Result updateQuartz(String id, String cron){
        Result result = new Result();
        result.setResult(false);
        try {
            CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(cron);
        }catch (Exception e){
            result.setMsg("cron表达式有误！！");
            return result;
        }
        try {
            tableService.updateCron(id,cron);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }


}
